﻿using B.S.Ingredients.APIRead.Application.Command.SlanBoss;
using B.S.Ingredients.APIRead.DTOs;
using B.S.Ingredients.Domain;
using B.S.Ingredients.ErrorCode;
using B.S.Ingredients.Infartructrue.Interfaces;
using MediatR;
using Microsoft.EntityFrameworkCore;

namespace B.S.Ingredients.APIRead.Application.CommandHandler.SlanBossHandler
{
    public class SlanBossQueryCommandHandler : IRequestHandler<SlanBossQueryCommand, APIResult<List<SlanDTO>>>
    {
        public SlanBossQueryCommandHandler(IBaseRepository<SlanghtSheng> SlanghtShengRepository)
        {
            this.SlanghtShengRepository = SlanghtShengRepository;
        }

        public IBaseRepository<SlanghtSheng> SlanghtShengRepository { get; }

        public async Task<APIResult<List<SlanDTO>>> Handle(SlanBossQueryCommand request, CancellationToken cancellationToken)
        {
           var path= await GetSlanList(request.Id);
            return new APIResult<List<SlanDTO>>
            {
                Code = ResultCode.success,
                Mag = "",
                Data = path
            };
        }

        public async Task<List<SlanDTO>> GetSlanList(int id)
        {
            var pathlist = SlanghtShengRepository.GetAll().Where(a => a.StaId==id).ToList();
            List<SlanDTO> dTOs = new List<SlanDTO>();
            foreach (var item in pathlist)
            {
                SlanDTO slan = new SlanDTO()
                {
                    Sid = item.Sid,
                    Sname = item.Sname,
                    Chell = await GetSlanList(item.Sid)
                };
                dTOs.Add(slan);
            }
            return dTOs;
        }
    }
}
